package controller;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import pojo.Employee;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 徒有琴
 */
@Controller
public class EmpController {
//    @InitBinder
//    public void initBinder(ServletRequestDataBinder binder) {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        binder.registerCustomEditor(Date.class,
//                new CustomDateEditor(dateFormat, true));
//    }

    @RequestMapping("date.html")
    public String date(Date d, @CookieValue("JSESSIONID")String id) {
        System.out.println(id);
        System.out.println(d);
        return "emp";
    }

    @RequestMapping("emp.html")
    public String addEmp(@Validated Employee employee, BindingResult result, Model model) {
//        if (result.hasErrors()) {//如果验证错误
//            FieldError nameError = result.getFieldError("name");
//            FieldError salaryError = result.getFieldError("salary");
//            if (nameError != null) {
//                model.addAttribute("nameError", nameError.getDefaultMessage());
//            }
//            if (salaryError != null) {
//                model.addAttribute("salaryError", salaryError.getDefaultMessage());
//            }
//            return "emp";
//        }

        System.out.println(employee);
        return "emp";
    }
}
